#include "widgethistorique.h"
#include "ui_widgethistorique.h"
#include "fenetreimage.h"
#include "historique.h"

using namespace std;

WidgetHistorique::WidgetHistorique(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::WidgetHistorique)
{
    mw = (MainWindow*) parent;
    ui->setupUi(this);
}

WidgetHistorique::~WidgetHistorique()
{
    delete ui;
}

void WidgetHistorique::reactu() {
    if(mw->getMdiArea()->activeSubWindow()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
        Historique* historique = &fi->historique;
        vector<ElementHistorique*>* pileAnnuler = historique->getPileAnnuler();
        vector<ElementHistorique*>::reverse_iterator It;
        ui->listWidget->clear();
        for(It = pileAnnuler->rbegin(); It != pileAnnuler->rend(); ++It) {
            QListWidgetItem* itm = new QListWidgetItem(*(*It)->getInfo());
            ui->listWidget->addItem(itm);
        }
    }
}
void WidgetHistorique::refaire(ElementHistorique *elm) {
    if(mw->getMdiArea()->activeSubWindow()) {
        FenetreImage* fi = (FenetreImage*) mw->getMdiArea()->activeSubWindow()->widget();
        Historique* historique = &fi->historique;
        QListWidgetItem* itm = new QListWidgetItem(*elm->getInfo());
        ui->listWidget->addItem(itm);

        vector<ElementHistorique*>* pileRetablir = historique->getPileRetablir();
        mw->getUi()->actionRefaire->setEnabled(! pileRetablir->empty());

        vector<ElementHistorique*>* pileAnnuler = historique->getPileAnnuler();
        mw->getUi()->actionAnnuler->setEnabled(!pileAnnuler->empty());
    }

}



void WidgetHistorique::paintEvent(QPaintEvent *)
{
    if(mw->getMdiArea()->activeSubWindow() == 0) {
        this->setEnabled(false);
    } else {
        this->setEnabled(true);
    }
}
